/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 *
 * MpmProjectRiskService
 * 项目风险
 *
 * 修改纪录
 * 2021-08-19 版本：1.0 xinfushuo 创建。
 * @version 版本：1.0
 * @author 作者：xinfushuo
 * 创建日期：2021-08-19
 */
package com.topscomm.mpm.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.topscomm.mpm.pojo.MpmProjectEntity;
import com.topscomm.mpm.pojo.MpmProjectProblemEntity;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.common.DatacodeCache;
import com.topscomm.tap.common.SessionUserBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mpm.service.IMpmProjectRiskService;
import com.topscomm.mpm.service.impl.auto.MpmProjectRiskServiceAuto;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.mpm.pojo.MpmProjectRiskEntity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(value = "mpmProjectRiskService")
@Scope(value = "singleton")
public class MpmProjectRiskService extends MpmProjectRiskServiceAuto implements IMpmProjectRiskService {


    @Autowired
    DatacodeCache datacodeCache;

    /**
     * @author: xinfushuo
     * @date: 2021-08-19
     * @param entity
     * @description:风险id 编号生成
     * @modify:
     */
    @Override
    protected void beforeInsert(BasicEntity entity) {
        super.beforeInsert(entity);
        //  赋值Id和单号和项目编号
        GenerateIDAndCode(entity,"PR",(MpmProjectRiskEntity.FieldRiskcode));
    }
    /**
      * @description: 赋值编号 docno改为提交时创建
      * @author xinfushuo
      * @param
      * @throws
      * @return
      * @date 2021/9/10
      * @modify
      */
    @Override
    protected void beforeSubmit(TapEntity entity, SessionUserBean sessionUserBean){
        //  赋值单号和项目编号
        super.beforeSubmit(entity,sessionUserBean);
        GenerateIDAndCode(entity,"PR",(MpmProjectRiskEntity.FieldRiskcode));
    }

    /**
     * @author: xinfushuo
     * @date: 2021-08-19
     * @param mapRow
     * @description:设置字段显示值
     * @modify:
     */
    @Override
    public void setDispFields(Map<String, Object> mapRow, DatacodeCache datacodeCache) {
        super.setDispFields(mapRow, datacodeCache);
        String temp;
        //流程名字显示
        if (mapRow.get("flowid") != null) {
            temp = mapRow.get("flowid").toString();
            mapRow.put("dispflowid", datacodeCache.getValueDynamic("ApprovalFlow_MpmProjectRisk", temp, "id", "name"));
        }
        //项目风险可能性显示
        if (mapRow.get("riskprobability") != null) {
            temp = mapRow.get("riskprobability").toString();
            mapRow.put("dispriskprobability", datacodeCache.getValueDynamic("MpmProjectRiskProbability", temp, "code", "name"));
        }
        //项目影响等级显示
        if (mapRow.get("riskinfluencelevel") != null) {
            temp = mapRow.get("riskinfluencelevel").toString();
            mapRow.put("dispriskinfluencelevel", datacodeCache.getValueDynamic("MpmProjectRiskInfluenceLevel", temp, "code", "name"));
        }

        //项目风险等级显示
        if (mapRow.get("risklevel") != null) {
            temp = mapRow.get("risklevel").toString();
            mapRow.put("disprisklevel", datacodeCache.getValueDynamic("MpmProjectRiskLevel", temp, "code", "name"));
        }
        //项目风险性质显示
        if (mapRow.get("riskproperty") != null) {
            temp = mapRow.get("riskproperty").toString();
            mapRow.put("dispriskproperty", datacodeCache.getValueDynamic("MpmProjectRiskProperty", temp, "code", "name"));
        }
        //项目风险趋势显示
        if (mapRow.get("risktrend") != null) {
            temp = mapRow.get("risktrend").toString();
            mapRow.put("disprisktrend", datacodeCache.getValueDynamic("MpmProjectRiskTrend", temp, "code", "name"));
        }
        //配置项目等级显示值
        if (mapRow.get("projectlevel") != null) {
            temp = mapRow.get("projectlevel").toString();
            mapRow.put("dispprojectlevel", datacodeCache.getValueDynamic("MpmProjectLevel", temp, "code", "name"));
        }
        //配置风险状态显示值
        if (mapRow.get("riskstatus") != null) {
            temp = mapRow.get("riskstatus").toString();
            mapRow.put("dispriskstatus", datacodeCache.getValueDynamic("MpmProjectRiskStatus", temp, "code", "name"));
        }
    }
    /**
     * @author: houyuhang
     * @date: 2022-09-02
     * @description:完成风险审批流
     * @modify:
     */
    public void finishRiskFlow(MpmProjectEntity mpmProjectEntity) {
        // 查找与该项目关联的风险单据
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" enabled = 1 and {0} = '{1}' ", MpmProjectRiskEntity.FieldProjectid, mpmProjectEntity.getId());
        List<MpmProjectRiskEntity> mpmProjectRiskEntities = this.queryByWhere(sql.toString());
        if (CollectionUtil.isEmpty(mpmProjectRiskEntities)) {
            return;
        }
        // 为风险单据赋值，模拟通过审批
        List<BasicEntity> basicEntities = new ArrayList<>();
        for (MpmProjectRiskEntity mpmProjectRiskEntity : mpmProjectRiskEntities) {
            mpmProjectRiskEntity.setState(9);
            mpmProjectRiskEntity.setLatestphase("结束");
            mpmProjectRiskEntity.setProjectname(mpmProjectEntity.getProjectname());
            mpmProjectRiskEntity.setProjectlevel(mpmProjectEntity.getProjectlevel());
            mpmProjectRiskEntity.setProjectcode(mpmProjectEntity.getProjectcode());
            //  赋值riskcode和docno
            GenerateIDAndCode(mpmProjectRiskEntity,"PR",(MpmProjectRiskEntity.FieldRiskcode));
            basicEntities.add(mpmProjectRiskEntity);
        }
        this.updateBatch(basicEntities);
    }
}
